package com.example.sdk.filter;

import com.example.sdk.Constants;
import com.example.sdk.exception.MoPlusException;
import com.example.sdk.utils.*;
import lombok.extern.log4j.Log4j2;

/**
 * Created by roger on 2019/1/8.
 */
@Log4j2
public class AESSecProcessor {

    private final static String CHARSET = "utf-8";

    private final static String key = "11111111111111111111111111111111";
    private String privateKey;//aeskey
    private byte[] aesKey;
    private byte[] rawData;
    private String data;

    public AESSecProcessor(String privateKey) {
        this.privateKey = privateKey;
    }

    public byte[] getAesKey() {
        return aesKey;
    }

    public byte[] getRawData() {
        return rawData;
    }



    public String getData() {
        return data;
    }


    public void processWithTimestamp(byte[] originalData) throws MoPlusException {
        try {

            // AES解密
            byte[] twoStrResult = AESUtils.parseHexStr2Byte(new String(originalData));

            // 解密
            byte[] rawData = AESUtils.decrypt(twoStrResult, privateKey);

            data = new String(rawData, CHARSET);
            log.debug("7.Request Info is:" + data);
        } catch (Exception e) {
            log.error("Error occured while process the encrypted data.", e);
            throw new MoPlusException(Constants.SYS_PROCESS_ENCRYPTED_DATA_FAILED, Constants.SYS_PROCESS_ENCRYPTED_DATA_FAILED_DESC);
        }
    }
}
